<?php
/**
 * --------------------------------------------------------------------------------
 * Author: Justin Zajac
 * Date: Feb 5, 2007
 * Description: 
 *
 * HANDMADE BY SPUTNIK AGENCY
 * Upstairs, 116-122 Chapel St
 * Windsor  VIC 3181
 * (Enter via Duke St)
 * VICTORIA AUSTRALIA
 *
 * PHONE +61 3 95344333
 * FAX    +61 3 95344833
 * EMAIL INFO@SPUTNIKAGENCY.COM
 * WEB   WWW.SPUTNIKAGENCY.COM
 * --------------------------------------------------------------------------------
 */
require_once('Reporter.php');


class XMLReporter extends Reporter implements iReporter{

	private $domDoc;
	
	public function __construct()
	{
		$this->domDoc = new DomDocument('1.0');
	}

	public function report($sourceObject, $configBlock = false)
	{
		$root = $this->domDoc->createElement('root');
		$root = $this->domDoc->appendChild($root);
		if($configBlock)
		{
			$this->getXMLforType($configBlock,"config", $root);
		}
		$this->getXMLforType($sourceObject,"object", $root);

		$this->domDoc->normalizeDocument();
		$xml_string = $this->domDoc->saveXML();
		return $xml_string;
	}
	
	function getXMLforType($item, $name, $currentElement)
	{	
			$child = $this->domDoc->createElement($this->getXMLSafeName($name));
			$child = $currentElement->appendChild($child);
			
			switch(getType($item))
			{
			case "object":
				$this->addObject2xml($item, $child);
				break;
			
			case "array":
				$this->addArray2xml($item, $child);
				break;
			
			default:
				$this->addProperty2xml($item, $child);
			}	
	}
	function addObject2xml($object, $parentElement)
	{
			$parentElement->setAttribute("id", $object->id);
			$objectVars = get_object_vars($object);
			foreach($objectVars as $key=>$item)
			{
				if(substr($key, 0 ,2)!="__" && !strpos($key, "private"))
				{
					$this->getXMLforType($item,$key, $parentElement);
				}
			}
	}
	function addArray2xml($array, $parentElement)
	{
		
		foreach($array as $key=>$item)
		{
			$this->getXMLforType($item,$key, $parentElement);
		}
	}
	function addProperty2xml($property, $parentElement)
	{
		$value = $this->domDoc->createTextNode($property);
		$value = $parentElement->appendChild($value);
	}


	function getXMLSafeName($name)
	{
		$name = eregi_replace(" ", "_", $name);
		if(eregi("^xml", $name) || !eregi("^[a-zA-Z]", $name))
		{
			//return "item_".$name;
			return "item";
		}else{
			return $name;
		}
	
	}
//    function __construct($properties = array()) {
//        parent::__construct($properties);
//    }

}
 
?>
